package com.example.mall.web.controller;

import com.example.mall.domain.Login;
import com.example.mall.domain.UserMemberships;
import com.example.mall.dto.UserMembershipsDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.PageDTO;
import com.example.mall.query.UserMembershipsQuery;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IUserMembershipsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userMemberships")
public class UserMembershipsController {

    @Autowired
    public IUserMembershipsService userMembershipsService;

    /**
     * 分页查询用户会员表信息
     * @param userMembershipsQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询用户会员表信息",notes = "分页查询用户会员表信息",httpMethod = "GET")
    public JSONResult queryPage(UserMembershipsQuery userMembershipsQuery){
        PageDTO<UserMembershipsDto> userMembershipsDtoPageDTO = userMembershipsService.queryPage(userMembershipsQuery);
        return JSONResult.success("查询成功",userMembershipsDtoPageDTO);
    }

    /**
     * 根据id查询用户会员表信息
     * @param membershipId
     * @return
     */
    @GetMapping("query/{membershipId}")
    @ApiOperation(value = "根据id查询用户会员表信息",notes = "根据id查询用户会员表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("membershipId")Long membershipId){
        //1.根据id查询用户信息
        if (membershipId == null){
            throw new GlobalCustomerException(ErrorEnumCode.MEMBERSHIPID_NOT_ID);
        }
        UserMemberships userMemberships = userMembershipsService.getById(membershipId);
        return JSONResult.success("查询成功",userMemberships);
    }

    /**
     * 根据id查询用户会员表信息
     * @param membershipId
     * @return
     */
    @GetMapping("queryUserMembershipsById/{membershipId}")
    @ApiOperation(value = "根据id查询用户会员表信息",notes = "根据id查询用户会员表信息",httpMethod = "GET")
    public JSONResult queryUserMembershipsById(@PathVariable("membershipId")Long membershipId){
        //1.根据id查询用户信息
        if (membershipId == null){
            throw new GlobalCustomerException(ErrorEnumCode.MEMBERSHIPID_NOT_ID);
        }
        UserMemberships userMemberships = userMembershipsService.queryUserMembershipsById(membershipId);
        return JSONResult.success("查询成功",userMemberships);
    }

    /**
     * 查询全部用户会员表信息
     * @param
     * @return
     */
    @GetMapping("queryUserMembershipsList")
    @ApiOperation(value = "查询全部用户会员表信息",notes = "查询全部用户会员表信息",httpMethod = "GET")
    public JSONResult queryUserMembershipsList(){
        List<UserMemberships> logins = userMembershipsService.queryUserMembershipsList();
        return JSONResult.success("查询成功",logins);
    }

    /**
     * 新增用户会员表
     * @param userMembershipsDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增用户会员表",notes = "新增用户会员表",httpMethod = "POST")
    public JSONResult insert(@RequestBody UserMembershipsDto userMembershipsDto){
        userMembershipsService.insert(userMembershipsDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改用户会员表信息
     * @param userMembershipsDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改用户会员表信息",notes = "修改用户会员表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody UserMembershipsDto userMembershipsDto){
        userMembershipsService.update(userMembershipsDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除用户会员表信息
     * @param membershipId
     * @return
     */
    @PostMapping("delete/{id}")
    @ApiOperation(value = "删除用户会员表信息",notes = "删除用户会员表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("membershipId") Long membershipId) {
        userMembershipsService.delete(membershipId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除用户会员表信息
     * @param membershipIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除用户会员表信息",notes = "批量删除用户会员表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> membershipIds) {
        userMembershipsService.deleteBatch(membershipIds);
        return JSONResult.success("批量删除成功");
    }
}
